<h4>w2ui best</h4>

<link rel="stylesheet" type="text/css" href={{ asset('assets/grid/temp/w2ui/dist/w2ui.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap/bootstrap.min.css') }} />


<div id="grid" style="width: 100%; height: 400px;"></div>
<br>
<button class="w2ui-btn" onclick="showChanged()">Get Changed</button>

<br>
<br>

<div id="sample" class="hhhholdjs"></div>


<script src="{{ asset('assets/jquery/jquery-1.10.2.min.js') }}"></script>
<script src="{{ asset('assets/grid/temp/w2ui/dist/w2ui.js') }}"></script>

<script type="text/javascript">
var books = {{ json_encode($book)}};
$(function () {
    var tax_kbn_list = {"1":"仕カ","2":"課売","3":"税外","4":"清算","5":"非売","6":"免売"};   // 税区分
    var tax_calc_kbn_list = {"1":"(","2":"+","3":"-"};   // 税計算区分
    var tax_calc_kbn_list2= {"1":"内税","2":"外税","3":"別記"};   // 税計算区分
    var records=[];
    var _recid = 0;
    var _acc={name:'grid',l2:'_l2'};
    function createSubLine1(id, e) {
        return { recid:''+id,ymd:e.ymd,d_code:e.d_code,d_name:e.d_name,d_amount:e.d_amount,d_tax:e.d_tax_pct,c_code:e.c_code,c_name:e.c_name, c_amount:e.c_amount,c_tax:e.c_tax_pct,summary:e.summary,data:e};
    };
    function createSubLine2(id, e) {
        return {recid:id+_acc.l2,d_code:e.d_code2,d_name:e.d_name2,d_amount:e.d_amount_tax,d_tax:e.d_tax_kbn,c_code:e.c_code2,c_name:e.c_name2,c_amount:e.c_amount_tax,c_tax:e.c_tax_kbn,summary:e.memo,data:e};
    };
    function getid(recid,index) {
        recid = (''+recid).replace(new RegExp(_acc.l2+'$'), '');
        return (index==0) ? 'grid_'+ _acc.name +'_rec_'+ w2utils.escapeId(recid):'grid_'+ _acc.name +'_rec_'+ w2utils.escapeId(recid+_acc.l2);
    };
    function selectToggle(grid, recid, on) {
        var index = grid.get(recid, true);
        var id = null;
        switch(index%2) {
            case 0:index++;id=getid(recid,1);break;
            case 1:index--;id=getid(recid,0);break;
            default:return;
        }
        var sel = grid.last.selection;
        var i = sel.indexes.indexOf(index);
        if (on && i == -1) {
             sel.indexes.push(index);
             var e  = $('#'+id);
             e.addClass('w2ui-selected').data('selected', 'yes').find('.w2ui-col-number').addClass('w2ui-row-selected');
             e.find('input.w2ui-grid-select-check').prop('checked', true);
        } else if (!on && i!= -1) {
            sel.indexes.splice[sel.indexes.indexOf(index),1];
            var e  = $('#'+id);
            e.removeClass('w2ui-selected').removeData('selected').find('.w2ui-col-number').removeClass('w2ui-row-selected');
            e.find('input.w2ui-grid-select-check').prop('checked', false);
        }
    };
    books.forEach(function(e){records.push(createSubLine1(_recid, e),createSubLine2(_recid++, e));});
    function render_tax(rec, i, c, tax, calckbn) {
        if (!tax) { return ''};
        if (i%2==0) { 
            $s = (tax_calc_kbn_list[calckbn]) ? tax_calc_kbn_list[calckbn] : '';
            return $s +tax+'%'
        } else {
            return (tax_kbn_list[tax]) ? tax_kbn_list[tax] : '';
        }
    };
    
    var codeEdit= {
        type: 'list',
        url : '{{route('acc.master.code.search')}}',
        onRequest : function (event) {
            event.postData.q=event.postData.search;
        },
        onLoad : function (event) {
            if ($.isArray(event.data)) {
                var list = [];
                for (var m = 0; m < event.data.length; m++) {
                    list[m]={ id: event.data[m].code, text: event.data[m].name, data : event.data[m] };
                }
                event.data = {status:'success', items : list};
            }
            event.onComplete = function (event) {};
        },
        renderItem: function (item) {
        console.log(2);
            return item.id + ' ' + item.text;
        },
        renderDrop: function (item) {
            return item.id  + ':' + item.text;
        },
        compare: function (item, search) {
          return search.indexOf(item.id)==0;
        }
    };

    w2grid = $('#grid').w2grid({ 
        name: 'grid', 
        show: { 
            toolbar: true,
            expandColumn    : false,
            toolbarAdd      : true,
            toolbarEdit     : true,
            toolbarDelete   : true,
            toolbarSave     : true,
            footer          : true,
            selectColumn    : true,
            multiSelect     : true,
        },
        keyboard : true,
        columnGroups:[
            { span: 1, caption: '#', master: true },
            { span: 1, caption: 'date', master: true },
            { span: 4, caption: 'Credit' },
            { span: 4, caption: 'Debt' },
            { span: 1, caption: 'Summary', master: true }
        ],
        columns: [
            { field: '', caption: '#', size: '24px', sortable: true, resizable: true , render: function (rec, i, c) {return (i%2==0) ? (i/2+1):'';} },
            { field: 'ymd', caption: 'date', size: '70px', sortable: true, resizable: true, editable: { type: 'text' }},
            
            //{ field: 'd_section', caption: 'sect', size: '45px', sortable: true, resizable: true, editable: { type: 'text' }},
            //{ field: 'd_section_name', caption: 'sect', size: '100px', sortable: true, resizable: true, editable: { type: 'text' }},
            { field: 'd_code', caption: 'code', size: '45px', sortable: true, resizable: true, editable: codeEdit},
            { field: 'd_name', caption: 'name', size: '100px', sortable: true, resizable: true, editable: { type: 'text' }},
            { field: 'd_amount', caption: 'amount', size: '80px', sortable: true, resizable: true, editable: { type: 'int' },render: 'int'},
            { field: 'd_tax', caption: '税', size: '40px', sortable: true, resizable: true, editable: { type: 'int' },render: function (rec, i, c){return render_tax(rec,i,c,rec.d_tax,rec.data.d_tax_calc_kbn);}},
            
            //{ field: 'd_tax_pct_kbn', caption: 'pkbn', size: '10px', sortable: true, resizable: true, editable: { type: 'int' },render: 'int'},

            //{ field: 'c_section', caption: 'sect', size: '45px', sortable: true, resizable: true, editable: { type: 'text' }},
            //{ field: 'c_section_name', caption: 'sect', size: '100px', sortable: true, resizable: true, editable: { type: 'text' }},
            { field: 'c_code', caption: 'code', size: '45px', sortable: true, resizable: true, editable: codeEdit},
            { field: 'c_name', caption: 'name', size: '100px', sortable: true, resizable: true, editable: { type: 'text' }},
            { field: 'c_amount', caption: 'amount', size: '80px', sortable: true, resizable: true, editable: { type: 'int' },render: 'int'},
            { field: 'c_tax', caption: '税', size: '40px', sortable: true, resizable: true, editable: { type: 'int' },render: function (rec, i, c){return render_tax(rec,i,c,rec.c_tax,rec.data.c_tax_calc_kbn);}},
            
            //{ field: 'c_tax_pct_kbn', caption: 'pkbn', size: '10px', sortable: true, resizable: true, editable: { type: 'int' },render: 'int'},

            { field: 'summary', caption: 'summary', size: '200px', sortable: true, resizable: true, editable: { type: 'text' }},
            
        ],
        records: records,
        onAdd: function (event) {
            var data = {}
            w2ui['grid'].add([{ recid:''+_recid, data:data}, { recid:_recid+_acc.l2,data:data}]);
            $('#'+getid(_recid++,0)).focus();
        },
        onDelete: function (event) {
            var indexes = this.last.selection.indexes;
            $.each(indexes, function(i, e) {
                if (e%2==0) {e++;} else {e--;};
                if (indexes.indexOf(e)==-1) {
                    indexes.push(e);
                }
            });
        },
        onSelect: function (event) {
            //event.onComplete = function () {selectToggle(this, event.recid, true);}
            //selectToggle(this, event.recid, true);
        },
        onUnselect: function (event) {
            //event.onComplete = function () {selectToggle(this, event.recid, false);}
            //selectToggle(this, event.recid, false);
        },
        onResize: function(event) {
            event.onComplete = function () {$(this.box).find('input.w2ui-grid-select-check:odd(:visible)').hide();}
        },
        onExpand2: function (event) {
            console.log(event);
        },
    });
});

</script>
<!--
    'mappings' => array(
        'type'                 => array('通常取引'=>0,'開始振替'=>1,'決算振替'=>2),      // 取引種類
        'deal'                 => array('振替'=>0,'預金'=>1),              // 伝票種類
        'approved'             => array('済'=>1),                          // 承認
        'checked'              => array('済'=>1),                          // チェック
        'd_tax_calc_kbn'       => array('内税'=>1,'外税'=>2,'別記'=>3),    // 借方税計算区分
        'c_tax_calc_kbn'       => array('内税'=>1,'外税'=>2,'別記'=>3),    // 貸方税計算区分
        'd_tax_kbn'            => array('仕カ'=>1,'課売'=>2,'税外'=>3,'清算'=>4,'非売'=>5,'免売'=>6),   // 借方税区分
        'c_tax_kbn'            => array('仕カ'=>1,'課売'=>2,'税外'=>3,'清算'=>4,'非売'=>5,'免売'=>6),   // 貸方税区分
        'history'              => array('訂正伝票(最新)'=>null),           // 履歴番号
    ),
//http://localhost/acc/book/entry/201108-1?l=b1
-->
